package com.changyinBus.web.controller.web;

import com.changyinBus.common.core.controller.BaseController;
import com.changyinBus.common.core.domain.AjaxResult;
import com.changyinBus.web.controller.bean.UrlSchemeRequest;
import com.changyinBus.web.controller.service.WeChatMiniProgramService;
import com.changyinBus.web.controller.weChartPay.bean.WeChatPayConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/web/")
@Slf4j
public class TestApi extends BaseController {
    @Resource
    private WeChatMiniProgramService miniProgramService;

    @GetMapping("/test")
    public ModelAndView test() {
        return new ModelAndView("test");
    }

    @GetMapping("/miniprogram/config")
    public AjaxResult getMiniProgramConfig(@RequestParam String path) {
        try {
            Map<String, String> config = miniProgramService.generateMiniProgramConfig(path);
            return success(config);
        } catch (Exception e) {
            return error("获取配置失败: " + e.getMessage());
        }
    }

    /**
     * 生成小程序跳转URL
     */
    @PostMapping("/miniprogram/url-link")
    public AjaxResult generateUrlScheme(@RequestBody UrlSchemeRequest request) {
        try {
            String urlLink = miniProgramService.generateUrlLink(request);
            System.out.println("urlLink:"+urlLink);
            return success(urlLink);
        } catch (Exception e) {
            log.error("生成URL Scheme失败", e);
            return error("生成失败: " + e.getMessage());
        }
    }
}
